package lovo01.lesson06;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * 发牌器
 */
public class DouDiZhu {

    ArrayList<PuKe> newArrayList = new ArrayList<>();
    /**
     * 初始化54章扑克牌
     */
    public void init() {
        newArrayList.add(new PuKe("","大王",15));
        newArrayList.add(new PuKe("","小王",14));

        String point = null;
        String type = null;
        int size = 0;
        for (int i = 0;i < 4;i++){
            if (i == 0){
                type = "♠";
            }else if (i == 1){
                type = "♥";
            }else if (i == 2){
                type = "♣";
            }else if (i == 3){
                type = "♦";
            }
            for (int j = 2;j < 15;j++){
                point = j + "";
                size = j;
                if (j == 11){
                    point = "j";
                }else if (j == 12){
                    point = "Q";
                }else if (j == 13){
                    point = "K";
                }else if (j == 14){
                    point = "A";
                }else if (j == 2){
                    size = 15;
                }
                newArrayList.add(new PuKe(point,type,size));
            }
        }
    }

    /**
     * 洗牌
     */
    public void shuffle(){
        Collections.shuffle(newArrayList);
    }

    /**
     * 发牌
     */
    public void sendCard(){
        ArrayList<PuKe> player1 = new ArrayList<>(newArrayList.subList(0,17));
        ArrayList<PuKe> player2 = new ArrayList<>(newArrayList.subList(17,34));
        ArrayList<PuKe> player3 = new ArrayList<>(newArrayList.subList(34,51));
        ArrayList<PuKe> cardHand = new ArrayList<>(newArrayList.subList(51,54));

        System.out.println("第一个人抓地主");
        System.out.println("地主牌为：" + cardHand);
        System.out.println("第一个玩家的牌：");
        for (PuKe puKe : player1){
            System.out.print(puKe + ",");
        }
        System.out.println();
        System.out.println("第二个玩家的牌：");
        for (PuKe puKe : player2){
            System.out.print(puKe + ",");
        }
        System.out.println();
        System.out.println("第三个玩家的牌：");
        for (PuKe puKe : player3){
            System.out.print(puKe + ",");
        }

        sort(player1);
        sort(player2);
        sort(player3);


    }

    /**
     * 手牌排序
     * @param arrayList
     */
    public void sort(ArrayList arrayList){
        Collections.sort(arrayList, new Comparator<PuKe>() {
            @Override
            public int compare(PuKe o1, PuKe o2) {
                return o1.getSize() - o2.getSize();
            }
        });
    }

    public static void main(String[] args) {
        DouDiZhu douDiZhu = new DouDiZhu();
        douDiZhu.init();
        douDiZhu.sendCard();
        douDiZhu.shuffle();
    }
}
